package com.example.controller;

import com.example.entity.ResponseEntity;
import com.example.entity.Student;
import com.example.service.StudentService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 学生对象控制类
 * @author 李平
 * @Date 2022-5-1
 */
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    private static final Logger LOG = LoggerFactory.getLogger(StudentController.class);

    /**
     * 学生角色登录接口
     * @param student
     * @return ResponseEntity
     */
    @PostMapping
    @ResponseBody
    @RequestMapping("/student-login")
    public ResponseEntity login(@RequestBody Student student){
        try {
            if (StringUtils.isEmpty(student.getPhone())){
                LOG.error("电话号码不能为空");
                return ResponseEntity.err("电话号码不能为空");
            }
            if (StringUtils.isEmpty(student.getPassword())){
                LOG.error("密码不能为空");
                return ResponseEntity.err("密码不能为空");
            }
            if (studentService.getStudentByPhone(student.getPhone()) == null){
                LOG.error(student.getPhone() + "无此用户");
                return ResponseEntity.err("无此用户");
            } else {
                // 登录校验业务
                Student stu = studentService.getStudentByPAndP(student.getPhone(), student.getPassword());
                if (stu != null){
                    LOG.info("用户登录成功：" + student.getPhone());
                    return  ResponseEntity.ok(stu);
                } else {
                    LOG.error(student.getPhone() + "密码错误");
                    return ResponseEntity.err("密码错误");
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            LOG.error("用户登录失败：" + student.getPhone());
            return ResponseEntity.err(e.getMessage());
        }
    }

    /**
     * 学生对象注册接口
     * @param student
     * @return ResponseEntity
     */
    @PostMapping
    @ResponseBody
    @RequestMapping("/student-register")
    public ResponseEntity register(@RequestBody Student student){
        try {
            if (student == null){
                LOG.error("学生对象不能为空");
                return ResponseEntity.err("学生对象不能为空");
            }
            // 注册业务
            Boolean b = studentService.addStudent(student);
            if (b) {
                // 当前电话号码未被注册，注册成功
                LOG.info(student.getPhone() + "注册成功");
                return ResponseEntity.ok(studentService.getStudentByPhone(student.getPhone()));
            }else {
                // 当前电话号码已被注册，无法注册
                LOG.error(student.getPhone() + "已被注册");
                return ResponseEntity.err(student.getPhone() + "已被注册");
            }
        }catch (Exception e) {
            e.printStackTrace();
            LOG.error("用户注册失败：" + student.getPhone());
            return ResponseEntity.err(e.getMessage());
        }
    }
}
